import pymysql
import struct
import datetime 
def wirteDB(data):

	#写入数据库


	conn = pymysql.connect("localhost", "root", "1234","test1")
	# 使用cursor()方法获取操作游标
	cursor = conn.cursor()

	#2. SQL 插入语句
	sql = """INSERT INTO data(idname, status,paska, lastActive) VALUES ( '%d', '%d', '%d', '%s')"""
	
	   # 执行sql语句
	print("123")
	cursor.execute(sql %(data[0], data[1],data[2],datetime.datetime.now()))
	conn.commit()
	print("success")
	   # 提交到数据库执行
	
	# 关闭数据库连接
	conn.close()

def dataParse(data):
	analy = []
	analy_1 = []
	datalen = len(data)
	print("长度",datalen)
	data = struct.unpack('B'*datalen,data)
	data=list(tuple(data))
	#print(type(data))
	for i in range(0, datalen -15):
		if data[i] == 0xAA and data[i+1] == 0x55 :
			print(data)
			ID = data[i+3] << 8 | data[i+2]
			#print(ID)
			Status = data[i+4]
			Paska = (data[i+8]+data[i+9]*256)*(data[i+6]+data[i+7]*256)
			analy.append(ID)
			analy.append(Status)
			analy.append(Paska)
			analy_1.append(analy)


	return analy_1

def main():
	data =b'\xaa\x55\x00\x02\x00\x01\x00\x92\xee\x96\xfc\x12\x13\x12\x15\x16'
	analy = dataParse(data)
	#print(analy)
	for dat in analy:
		print(dat)
		wirteDB(dat)
		print("ok")
if __name__ == '__main__':
	main()